package com.sfzd5.AnswerSheetJavaTest;

import com.google.zxing.WriterException;
import com.sfzd5.exam.helplibrary.QrcodeHelper;
import com.sfzd5.exam.helplibrary.WindowsDrawStringEventListener;
import com.sfzd5.exam.helplibrary.exam.ExamInof;
import com.sfzd5.exam.helplibrary.exam.MakePage;
import com.sfzd5.exam.helplibrary.exam.Page;
import com.sfzd5.exam.helplibrary.exam.answersheet.*;
import org.bytedeco.javacpp.opencv_core.*;
import org.bytedeco.javacv.Java2DFrameUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.awt.image.BufferedImage.TYPE_BYTE_GRAY;
import static org.bytedeco.javacpp.opencv_core.CV_8UC1;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;

public class MainClass {
    public static void main(String[] args){
        System.out.println("Hello wrold!");
        try {
            testMakePage();
            //testQRcode();
        } catch (IOException e) {

        }
    }

    public static void testQRcode() throws WriterException, IOException {
        Mat mat = QrcodeHelper.qrCodeEncode("Hello world!", 120);

        Mat bg = imread("saved.png", CV_8UC1);

        // 定义感兴趣区域(位置，logo图像大小)
        Mat ROI = bg.apply(new Rect(50, 50, mat.cols(), mat.rows()));

        ROI.put(new Scalar(120));
        mat.copyTo(ROI);

        Bitmap bmp = new Bitmap(bg);

        BufferedImage img = bmp.getImg();

        File outputfile = new File("qr.png");
        ImageIO.write(img, "png", outputfile);

    }

    private static WindowsDrawStringEventListener windowsDrawStringEventListener = new WindowsDrawStringEventListener() {
        @Override
        public Mat drawString(String str, float fontSize) {
            int fs = (int)Math.round(fontSize);
            int w = (int)(str.length() * fontSize * 1.5);
            int h = (int)(fontSize * 2);

            BufferedImage img = new BufferedImage(
                    w,
                    h,
                    TYPE_BYTE_GRAY);
            Graphics2D g = img.createGraphics();
            g.setColor(Color.WHITE);//设置笔刷白色
            g.fillRect(0,0, w, h);//填充整个屏幕

            Font font = new Font(g.getFont().getFontName(), Font.PLAIN, fs);
            g.setFont(font);
            g.setColor(Color.BLACK);
            FontMetrics fm = g.getFontMetrics();
            g.drawString(str, 0, fs - fm.getDescent());

            Rectangle2D rect = g.getFontMetrics().getStringBounds(str, g);
            g.dispose();

            BufferedImage strImg = img.getSubimage(0, 0, (int)rect.getWidth(), (int)rect.getHeight());
            return Java2DFrameUtils.toMat(strImg);
        }
    };


    public static void testMakePage() throws IOException {
        com.sfzd5.exam.helplibrary.DrawMatHelper.setWindowsDrawStringEventListener(windowsDrawStringEventListener);

        ExamInof examInof = new ExamInof();
        examInof.examName = "这是一次完美的测试";
        {
            //添加选择题
            Area choiceArea = new Area();
            choiceArea.areaType = Area.AreaType.Choice;
            choiceArea.title = "一、比较与选择（1—4小题单选，每题2分；5—8小题多选，每题3分，共20分）";
            for (int i = 0; i < 8; i++)
                choiceArea.answerSheetQuestionList.add(new ChoiceAnswerSheetQuestion());
            //choiceArea.lineCount = (int) Math.ceil(choiceArea.questionList.size() / 4d);
            examInof.areaList.add(choiceArea);
        }

        {
            //添加填空题
            Area blankArea = new Area();
            blankArea.areaType = Area.AreaType.Blank;
            blankArea.title = "二、填空题（每题空2分，共20分）";
            blankArea.answerSheetQuestionList = new ArrayList<>();
            List<BlankAnswerSheetQuestion> blankQuestionList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                BlankAnswerSheetQuestion blankQuestion = new BlankAnswerSheetQuestion();
                BlankInfo blankInfo1 = new BlankInfo();
                blankInfo1.width = 40;
                blankQuestion.getBlankInfoList().add(blankInfo1);
                blankQuestion.setBlankInfoList(blankQuestion.getBlankInfoList());
                BlankInfo blankInfo2 = new BlankInfo();
                blankInfo2.width = 40;
                blankQuestion.getBlankInfoList().add(blankInfo2);
                blankQuestion.setBlankInfoList(blankQuestion.getBlankInfoList());
                blankArea.answerSheetQuestionList.add(blankQuestion);
                blankQuestionList.add(blankQuestion);
            }
            //blankArea.lineCount = pdfHelper.getBlankLines(blankQuestionList).size();
            examInof.areaList.add(blankArea);
        }

        {
            Area expoundArea = new Area();
            expoundArea.areaType = Area.AreaType.Expound;
            expoundArea.title = "综合题（每题空2分，共20分）";
            expoundArea.answerSheetQuestionList = new ArrayList<>();
            ExpoundAnswerSheetQuestion expoundQuestion = new ExpoundAnswerSheetQuestion();
            {
                ExpoundChildQuestion ecq = new ExpoundChildQuestion();
                ecq.lineNum = 3;
                ecq.withLine = true;
                expoundQuestion.getChildQuestionList().add(ecq);
                expoundQuestion.setChildQuestionList(expoundQuestion.getChildQuestionList());
            }
            {
                ExpoundChildQuestion ecq = new ExpoundChildQuestion();
                ecq.lineNum = 3;
                ecq.withLine = true;
                expoundQuestion.getChildQuestionList().add(ecq);
                expoundQuestion.setChildQuestionList(expoundQuestion.getChildQuestionList());
            }
            {
                ExpoundChildQuestion ecq = new ExpoundChildQuestion();
                ecq.lineNum = 3;
                ecq.withLine = true;
                expoundQuestion.getChildQuestionList().add(ecq);
                expoundQuestion.setChildQuestionList(expoundQuestion.getChildQuestionList());
            }

            expoundArea.answerSheetQuestionList.add(expoundQuestion);

            examInof.areaList.add(expoundArea);
        }

        MakePage makePage = new MakePage();
        examInof.pages = makePage.makePages(examInof);

        Page p = examInof.pages.get(0);

        makePage.drawPage(examInof, p, examInof.taskId, 12);

        Mat mat = makePage.getMat();
        Bitmap bmp = new Bitmap(mat);
        BufferedImage img = bmp.getImg();

        File outputfile = new File("saved.png");
        ImageIO.write(img, "png", outputfile);
    }
}
